﻿#region Usings

using System;
using System.IO;
using System.Xml.XPath;

#endregion

namespace Kazkommertsbank.Framework.Model.Pay
{
    public class PaymentErrorDocument : ResponseDocumentBase
    {
        public PaymentErrorDocument(string response) : base(response)
        {}

        #region Overrides of DocumentBase

        public override KkbResponseType ResponseType
        {
            get { return KkbResponseType.Error; }
        }

        protected override bool ParseXml(string response)
        {
            try
            {
                XPathDocument doc = new XPathDocument(new StringReader(response));
                XPathNavigator nav = doc.CreateNavigator();

                XPathNavigator navigator = nav.SelectSingleNode("//response");
                if(navigator != null)
                    OrderId = navigator.GetAttribute("order_id", "");

                navigator = nav.SelectSingleNode("//response/error");
                if(navigator != null)
                {
                    Error = navigator.Value;
                    ErrorCode = navigator.GetAttribute("code", "");
                    ErrorTime = navigator.GetAttribute("time", "");
                    ErrorType = navigator.GetAttribute("type", "");
                }

                navigator = nav.SelectSingleNode("//response/session");
                if(navigator != null)
                    SessionId = navigator.GetAttribute("id", "");

                return true;
            }
            catch(Exception)
            {
                return false;
            }
        }

        #endregion

        public string OrderId { get; private set; }
        public string Error { get; private set; }
        public string ErrorType { get; private set; }
        public string ErrorTime { get; private set; }
        public string ErrorCode { get; private set; }
        public string SessionId { get; private set; }
    }
}